﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Data;
using System.IO;
using System.Web.Script.Serialization;
using System.Configuration;

namespace website.ProblemInfo
{
    /// <summary>
    /// ProblemInfo 的摘要说明
    /// </summary>
    public class ProblemInfo : IHttpHandler
    {
        string Action = "";
        JavaScriptSerializer jsc = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            Action = RequstString("Action");
            if (Action == "uploadimg")
            {
                ProblemInfo.returnresult obj = new ProblemInfo.returnresult();
                obj = this.uploadimage();
                context.Response.Write(this.jsc.Serialize(obj));
            }
            else if (Action == "uploadvideo")
            {
                HttpPostedFile httpPostedFile = context.Request.Files["file"];
                ProblemInfo.returnresult obj = new ProblemInfo.returnresult();
                obj = this.uploadvideo();
                context.Response.Write(this.jsc.Serialize(obj));
            }
            else if (Action == "getEquNoListByCustomerID")
            {
                context.Response.Write(this.getEquNoListByCustomerID());
            }
            else if (Action == "getCustomerListByHongDa")
            {
                context.Response.Write(this.getCustomerListByHongDa());
            }
            else if (this.Action == "getToUserNoByCustomerID")
            {
                context.Response.Write(this.getToUserNoByCustomerID());
            }
            else if (Action == "saveProblemInfo")
            {
                context.Response.Write(this.saveProblemInfo());
            }
            else if (Action == "saveMessageRepate")
            {
                context.Response.Write(this.saveMessageRepate());
            }
            else if (Action == "GetProblemInfo")
            {
                string sidx = ProblemInfo.RequstString("sidx");
                string sord = ProblemInfo.RequstString("sord");
                string toUserNo = ProblemInfo.RequstString("ToUserNo");
                string keyvalue = ProblemInfo.RequstString("keyword");
                context.Response.Write(this.GetProblemInfo(sidx, sord, toUserNo, keyvalue));
            }

            else if (Action == "GetProblemInfoSend")
            {
                string sidx = ProblemInfo.RequstString("sidx");
                string sord = ProblemInfo.RequstString("sord");
                string toUserNo = ProblemInfo.RequstString("ToUserNo");
                string keyvalue = ProblemInfo.RequstString("keyword");
                context.Response.Write(this.GetProblemInfoSend(sidx, sord, toUserNo, keyvalue));
            }

            else if (Action == "getVideoByProblemID")
            {
                string problemID = ProblemInfo.RequstString("ProblemID");
                context.Response.Write(this.getVideoByProblemID(problemID));
            }
            else if (Action == "getImgByProblemID")
            {
                string problemID = ProblemInfo.RequstString("ProblemID");
                context.Response.Write(this.getImgByProblemID(problemID));
            }
            else if (Action == "getMessageInfoByID")
            {
                string messageID = ProblemInfo.RequstString("MessageID");
                context.Response.Write(this.getMessageInfoByID(messageID));
            }
            else if (Action == "deleteProblemInfo")
            {
                string problemID = ProblemInfo.RequstString("ProblemID");
                context.Response.Write(this.deleteProblemInfo(problemID));
            }
        }

        public ProblemInfo.returnresult uploadimage()
        {
            ProblemInfo.returnresult returnresult = new ProblemInfo.returnresult();
            string text = HttpContext.Current.Server.MapPath("\\ProblemInfo\\uploadFilePath\\");
            HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files["file"];
            if (!Directory.Exists(text))
            {
                Directory.CreateDirectory(text);
            }
            string filename = text + httpPostedFile.FileName;
            try
            {
                httpPostedFile.SaveAs(filename);
                returnresult.code = 1;
                returnresult.msg = httpPostedFile.FileName;
            }
            catch (Exception)
            {
                returnresult.code = 0;
                returnresult.msg = "文件上传失败";
            }
            return returnresult;
        }

        // Token: 0x0600009B RID: 155 RVA: 0x000070C4 File Offset: 0x000052C4
        public ProblemInfo.returnresult uploadvideo()
        {
            ProblemInfo.returnresult returnresult = new ProblemInfo.returnresult();
            string text = HttpContext.Current.Server.MapPath("\\ProblemInfo\\uploadFilePath\\");
            HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files["file"];
            string text2 = httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(".") + 1, httpPostedFile.FileName.Length - httpPostedFile.FileName.LastIndexOf(".") - 1);
            if (text2.ToLower() != "mp4")
            {
                returnresult.code = 0;
                returnresult.msg = "只能上传MP4格式文件";
            }
            else
            {
                if (!Directory.Exists(text))
                {
                    Directory.CreateDirectory(text);
                }
                string filename = text + httpPostedFile.FileName;
                try
                {
                    httpPostedFile.SaveAs(filename);
                    returnresult.code = 1;
                    returnresult.msg = httpPostedFile.FileName;
                }
                catch (Exception)
                {
                    returnresult.code = 0;
                    returnresult.msg = "文件上传失败";
                }
            }
            return returnresult;
        }

        // Token: 0x0600009C RID: 156 RVA: 0x000071EC File Offset: 0x000053EC
        public string getEquNoListByCustomerID()
        {
            string customerID = ProblemInfo.RequstString("CustomerID");
            return CustomerMFGBLL.getEquNoListByCustomerID(customerID);
        }

        // Token: 0x0600009D RID: 157 RVA: 0x00007214 File Offset: 0x00005414
        public string getCustomerListByHongDa()
        {
            return CustomerMFGBLL.getCustomerListByHongDa();
        }

        // Token: 0x0600009E RID: 158 RVA: 0x00007230 File Offset: 0x00005430
        public string getToUserNoByCustomerID()
        {
            string customerID = ProblemInfo.RequstString("CustomerID");
            return CustomerMFGBLL.getToUserNoByCustomerID(customerID);
        }

        // Token: 0x0600009F RID: 159 RVA: 0x00007258 File Offset: 0x00005458
        public string saveProblemInfo()
        {
            string customerName = ProblemInfo.RequstString("CustomerName");
            string fromUserNo = ProblemInfo.RequstString("FromUserNo");
            string toUserNo = ProblemInfo.RequstString("ToUserNo");
            string problemIssue = ProblemInfo.RequstString("ProblemIssue");
            string problemImage = ProblemInfo.RequstString("ProblemImage");
            string videoPath = ProblemInfo.RequstString("VideoPath");
            return ProblemInfoBLL.saveProblemInfo(customerName, fromUserNo, toUserNo, problemIssue, problemImage, videoPath);
        }

        // Token: 0x060000A0 RID: 160 RVA: 0x000072C4 File Offset: 0x000054C4
        public string saveMessageRepate()
        {
            string messageID = ProblemInfo.RequstString("MessageID");
            string messageWordsRepate = ProblemInfo.RequstString("MessageWordsRepate");
            string fromUserNo = ProblemInfo.RequstString("FromUserNo");
            string toUserNo = ProblemInfo.RequstString("ToUserNo");
            string problemImage = ProblemInfo.RequstString("ProblemImage");
            string videoPath = ProblemInfo.RequstString("VideoPath");
            return ProblemInfoBLL.saveMessageRepate(messageID, messageWordsRepate, fromUserNo, toUserNo, problemImage, videoPath);
        }

        // Token: 0x060000A1 RID: 161 RVA: 0x00007330 File Offset: 0x00005530
        /// <summary>
        /// 收到的留言
        /// </summary>
        /// <param name="sidx"></param>
        /// <param name="sord"></param>
        /// <param name="ToUserNo"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public string GetProblemInfo(string sidx, string sord, string ToUserNo, string keyvalue)
        {
            DataTable problemInfo = ProblemInfoBLL.GetProblemInfo(sidx, sord, ToUserNo, keyvalue);
            string text2;
            if (problemInfo != null)
            {
                string text = ProblemInfo.RequstString("page");
                string value = ProblemInfo.RequstString("rows");
                int count = problemInfo.Rows.Count;
                int num = (count % (int)Convert.ToInt16(value) == 0) ? (count / (int)Convert.ToInt16(value)) : (count / (int)Convert.ToInt16(value) + 1);
                int num2 = (int)((Convert.ToInt16(text) - 1) * Convert.ToInt16(value));
                int num3 = (int)Convert.ToInt16(value);
                text2 = string.Concat(new object[]
				{
					"{\"page\":",
					text,
					",\"total\": ",
					num,
					"  ,\"records\":",
					problemInfo.Rows.Count.ToString(),
					",\"rows\":["
				});
                int num4 = num2;
                while (num4 < num3 + num2 && num4 < count)
                {
                    text2 += "{";
                    text2 = text2 + "\"id\":\"" + (num4 + 1).ToString() + "\",";
                    text2 += "\"cell\":";
                    text2 += "[";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ID"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["FormUserNo"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["MessageTime"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["FormUserName"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["MessageWords"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ProblemImage"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ProblemVideo"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["RepeateState"].ToString() + "\"";
                    text2 += "]";
                    text2 += "}";
                    if (num4 != num3 + num2 - 1 && num4 != count - 1)
                    {
                        text2 += ",";
                    }
                    num4++;
                }
            }
            else
            {
                text2 = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";
            }
            text2 = text2.Trim().TrimEnd(new char[]
			{
				','
			});
            return text2 + "]}";
        }

        /// <summary>
        /// 发送的留言
        /// </summary>
        /// <param name="sidx"></param>
        /// <param name="sord"></param>
        /// <param name="ToUserNo"></param>
        /// <param name="keyvalue"></param>
        /// <returns></returns>
        public string GetProblemInfoSend(string sidx, string sord, string ToUserNo, string keyvalue)
        {
            DataTable problemInfo = ProblemInfoBLL.GetProblemInfoSend(sidx, sord, ToUserNo, keyvalue);
            string text2;
            if (problemInfo != null)
            {
                string text = ProblemInfo.RequstString("page");
                string value = ProblemInfo.RequstString("rows");
                int count = problemInfo.Rows.Count;
                int num = (count % (int)Convert.ToInt16(value) == 0) ? (count / (int)Convert.ToInt16(value)) : (count / (int)Convert.ToInt16(value) + 1);
                int num2 = (int)((Convert.ToInt16(text) - 1) * Convert.ToInt16(value));
                int num3 = (int)Convert.ToInt16(value);
                text2 = string.Concat(new object[]
				{
					"{\"page\":",
					text,
					",\"total\": ",
					num,
					"  ,\"records\":",
					problemInfo.Rows.Count.ToString(),
					",\"rows\":["
				});
                int num4 = num2;
                while (num4 < num3 + num2 && num4 < count)
                {
                    text2 += "{";
                    text2 = text2 + "\"id\":\"" + (num4 + 1).ToString() + "\",";
                    text2 += "\"cell\":";
                    text2 += "[";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ID"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ToUserNo"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["MessageTime"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["MessageWords"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ProblemImage"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["ProblemVideo"].ToString() + "\",";
                    text2 = text2 + "\"" + problemInfo.Rows[num4]["RepeateState"].ToString() + "\"";
                    text2 += "]";
                    text2 += "}";
                    if (num4 != num3 + num2 - 1 && num4 != count - 1)
                    {
                        text2 += ",";
                    }
                    num4++;
                }
            }
            else
            {
                text2 = "{\"page\":1,\"total\":0,\"records\":0,\"rows\":[";
            }
            text2 = text2.Trim().TrimEnd(new char[]
			{
				','
			});
            return text2 + "]}";
        }

        // Token: 0x060000A2 RID: 162 RVA: 0x00007640 File Offset: 0x00005840
        public string getVideoByProblemID(string ProblemID)
        {
            return ProblemInfoBLL.getVideoByProblemID(ProblemID);
        }

        // Token: 0x060000A3 RID: 163 RVA: 0x0000765C File Offset: 0x0000585C
        public string getImgByProblemID(string ProblemID)
        {
            return ProblemInfoBLL.getImgByProblemID(ProblemID);
        }

        // Token: 0x060000A4 RID: 164 RVA: 0x00007678 File Offset: 0x00005878
        public string getMessageInfoByID(string MessageID)
        {
            return ProblemInfoBLL.getMessageInfoByID(MessageID);
        }

        // Token: 0x060000A5 RID: 165 RVA: 0x00007694 File Offset: 0x00005894
        public string deleteProblemInfo(string ProblemID)
        {
            return ProblemInfoBLL.deleteProblemInfo(ProblemID);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public static string RequstString(string sParam)
        {
            return (HttpContext.Current.Request[sParam] == null ? string.Empty
                  : HttpContext.Current.Request[sParam].ToString().Trim());
        }

        public class returnresult
        {
            public int code { get; set; }
            //public bool status { get; set; }
            public string msg { get; set; }
        }
    }
}